package com.mobile.ado.tabbar;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

import com.mobile.ado.R;

public abstract class TabbarActivity extends ActivityGroup {

	protected ImageView logoImageView;
	protected ImageView rightTopImageView;

	protected LinearLayout contentViewLayout;
	protected GridView toolbarGridView;

	protected ArrayList<Intent> intents;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		intents = getIntents();
		setContentView(R.layout.tabbar);

		setupTopView();

		setupMidContentView();

		setupBottomTabBarView();

		setCurrentTab(0);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	private SimpleAdapter getMenuAdapter(String[] menuNameArray,
			int[] imageResourceArray) {

		ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < menuNameArray.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("itemImage", imageResourceArray[i]);
			map.put("itemText", menuNameArray[i]);
			data.add(map);
		}
		SimpleAdapter simperAdapter = new SimpleAdapter(this, data,
				R.layout.item_menu, new String[] { "itemImage", "itemText" },
				new int[] { R.id.item_image, R.id.item_text });
		return simperAdapter;

	}

	private void setupBottomTabBarView() {
		toolbarGridView = (GridView) findViewById(R.id.toolbargridView);

		toolbarGridView.setSelector(R.drawable.toolbar_menu_item);
		toolbarGridView.setBackgroundResource(R.drawable.menu_bg2);
		toolbarGridView.setNumColumns(getTabNumber());
		toolbarGridView.setGravity(Gravity.CENTER);
		toolbarGridView.setVerticalSpacing(10);
		toolbarGridView.setHorizontalSpacing(10);
		toolbarGridView.setAdapter(getMenuAdapter(getMenuNameArray(),
				getImageResourceArray()));

		toolbarGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				setCurrentTab(position);
			}
		});
	}

	private void setupMidContentView() {
		contentViewLayout = (LinearLayout) findViewById(R.id.contentViewLayout);
	}

	private void setupTopView() {
		logoImageView = (ImageView) findViewById(R.id.logImage);
		rightTopImageView = (ImageView) findViewById(R.id.rightTopImage);

		rightTopImageView.setOnClickListener(getRightTopViewOnclickListener());

	}

	public void setCurrentTab(int index) {
		Intent intent = intents.get(index);
		View contentView = this.getLocalActivityManager().startActivity(
				"Activity" + index, intent).getDecorView();

		contentViewLayout.removeAllViews();
		contentViewLayout.addView(contentView, new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

		int[] rightTopViewVisibility = getRightTopViewVisibility();
		rightTopImageView.setVisibility(rightTopViewVisibility[index]);
	}

	protected abstract int getTabNumber();

	protected abstract String[] getMenuNameArray();

	protected abstract int[] getImageResourceArray();

	protected abstract ArrayList<Intent> getIntents();

	protected abstract OnClickListener getRightTopViewOnclickListener();

	protected abstract int[] getRightTopViewVisibility();
}
